<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">

    <span th:replace="common_page::common"></span>
</head>
<body>

<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" id="functionSearchForm" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">功能ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="functionId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">功能名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">连接地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="url" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">权限标志</label>
                            <div class="layui-input-inline">
                                <input type="text" name="permission" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">启用状态</label>
                            <div class="layui-input-inline">
                                <select name="enableStatus" lay-search="">
                                    <option value="" selected>请选择</option>
                                    <option value="1" >启用</option>
                                    <option value="0">禁用</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="reset" class="layui-btn layui-btn-primary" id="data-reset-btn" > 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>
            </div>
        </script>

        <table class="layui-hide" id="functionTreeTable" lay-filter="functionTreeTableFilter"></table>

        <!-- 操作列 -->
        <script type="text/html" id="functionTreeTableToolbar">
            <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
        </script>

    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script th:src="@{/js/common/selectApp.js}" charset="utf-8"></script>


<script>

    layui.config({
        base: basePath+'/lib/layui_ext/treeTable/' //配置 layui 第三方扩展组件存放的基础目录
    }).extend({
        treeTable: 'treeTable' //定义该组件模块名
    }).use(['form', 'table', 'treeTable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var treetable = layui.treeTable;
        var loadding;
        var treetableHandler;

        $.fn.serializeJson=function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        };

        function getFormParam(row)
        {
            var formParam = $("#functionSearchForm").serializeJson();
            if(row!=null) {
                formParam.pid = row.id;
                formParam.appCode = row.appCode;
            }
            return formParam;
        }

        $("#data-reset-btn").click(function(){
            $("#functionSearchForm")[0].reset();
            layui.form.render();
        });


        function loadTreeTable()
        {
            loadding = layer.load();
            treetableHandler = treetable.render({
                text: {},
                elem: '#functionTreeTable',
                toolbar: '#toolbarDemo',
                tree: {
                    iconIndex: 2
                },
                cols: [
                    {type: 'numbers'},
                    {type: 'checkbox'},
                    {field: 'name', width: 400,  title: '功能名称'},
                    {field: 'url', title: '链接'},
                    {field: 'permission',title: '权限'},
                    {field: 'icon',title: '图标'},
                    {field: 'functionSort', title: '排序'},
                    {field: 'createAdminUsername', title: '创建人'},
                    {field: 'createDate', title: '创建时间'},
                    {field: 'updateAdminUsername', title: '修改人'},
                    {field: 'updateDate',  title: '修改时间'},
                    {field: 'enableStatus', width: 160, align: 'center', title: '状态', templet: function (d) {
                            if (d.enableStatus == 1) {
                                return '启用';
                            } else {
                                return '禁用';
                            }
                        }},
                    {
                        field: 'type', width: 80, align: 'center', templet: function (d) {
                            if (d.type == 0) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            }else if (d.type == 1) {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }else  if (d.type == 2) {
                                return '<span class="layui-badge layui-bg-gray">操作按钮</span>';
                            }else  if (d.type == 3) {
                                return '<span class="layui-badge layui-bg-gray">工具条按钮</span>';
                            }else  if (d.type == 4) {
                                return '<span class="layui-badge layui-bg-gray">API</span>';
                            }else  if (d.type == 5) {
                                return '<span class="layui-badge layui-bg-gray">页面控件</span>';
                            }
                            return '<span class="layui-badge layui-bg-black">应用</span>';
                        }, title: '类型'
                    },
                    {templet: '#functionTreeTableToolbar', width: 150, align: 'center', title: '操作'}

                ],
                reqData: function (selectRow, callback) {
                    $.ajax({
                        url:"[[@{/}]]function/tree/table/by/pid",
                        type:'GET',
                        data:getFormParam(selectRow),
                        success:function(res){
                            callback(res.data);
                        },
                        complete:function(data)
                        {
                            layer.closeAll('loading');
                            layer.close(loadding);
                        }
                    });
                }
            });

        }

        loadTreeTable();

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            loadTreeTable();

            return false;
        });

        /**
         * toolbar监听事件
         */
        treetable.on('toolbar(functionTreeTable)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加功能',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: "[[@{/function/addPage}]]"
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var data = treetableHandler.checkStatus();
                if(data==null||data.length<=0)
                {
                    layer.msg("请选择要操作的记录");
                    return;
                }
                layer.confirm("确定删除功能项?", function (index) {
                    $.ajax({
                        url:"[[@{/}]]function/delete/ids",
                        contentType: "application/json; charset=utf-8",
                        type:'DELETE',
                        data:JSON.stringify(data),
                        success:function(data){
                            layer.msg(data.msg);

                            //执行搜索重载
                            loadTreeTable();

                        },
                        complete:function(data)
                        {
                            layer.close(index);
                        }
                    });
                });
            }
        });

        //监听表格复选框选择
        treetable.on('checkbox(functionTreeTableFilter)', function (obj) {
            console.log(obj)
        });

        treetable.on('tool(functionTreeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                if(data.type==null)
                {
                    layer.msg("不能修改该节点");
                    return false;
                }
                var index = layer.open({
                    title: '编辑功能',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: '[[@{/}]]function/editPage/'+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                if(data.type==null)
                {
                    layer.msg("不能删除该节点");
                    return false;
                }
                layer.confirm("确定删除"+data.name+"?", function (index) {
                    $.ajax({
                        url:"[[@{/}]]function/delete/"+data.id,
                        contentType: "application/json; charset=utf-8",
                        type:'DELETE',
                        success:function(data){
                            layer.msg(data.msg);

                            //执行搜索重载
                            loadTreeTable();

                        },
                        complete:function(data)
                        {
                            layer.close(index);
                        }
                    });
                });
            }
        });

    });
</script>

</body>
</html>